package com.itwk.gulimall.product.controller;

import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.R;
import com.itwk.gulimall.product.entity.PmsAttrEntity;
import com.itwk.gulimall.product.entity.PmsAttrGroupEntity;
import com.itwk.gulimall.product.service.PmsAttrGroupService;
import com.itwk.gulimall.product.vo.AttrGroupRelation;
import com.itwk.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author wangke
 * @email wangke19970512@163.com
 * @date 2021-01-07 23:03:03
 */
@RestController
@RequestMapping("/product/attrgroup")
public class PmsAttrGroupController {
    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
  //  @RequiresPermissions("product:pmsattrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catId){
       // PageUtils page = pmsAttrGroupService.queryPage(params);
        //需求为在展示品牌信息的时候可以点击分类属性直接获取到品牌信息，map中是封装的分页查询条件，由逆向工具的Query类解析，返回IPage对象
        PageUtils page = pmsAttrGroupService.queryCategoryPage(params,catId);
        return R.ok().put("page", page);
    }

     /*
     *  查询分组关联的基本属性请求  /product/attrgroup/{attrgroupId}/attr/relation
     * */
     @GetMapping("/{attrgroupId}/attr/relation")
     public  R getAttrGroupRelation(@PathVariable(name = "attrgroupId") Long attrgroupId ){
       List<PmsAttrEntity> attrGroupRelation = pmsAttrGroupService.getAttrgoupRelation(attrgroupId);
         return R.ok().put("data",attrGroupRelation);
     }
    /*
    * 删除基本属性与分组关联关系
    * /product/attrgroup/attr/relation/delete
    * */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrgroupRelation(@RequestBody AttrGroupRelation[] relations){
        pmsAttrGroupService.deleteRelation(relations);
        return R.ok();
    }

    /* 分组中新建属性关联关系，已经被关联的属性不会被返回
    * /product/attrgroup/{attrgroupId}/noattr/relation
    * */
    @GetMapping("/{attrgroupId}/noattr/relation")
     public R getNoGoupAttr(@RequestParam Map<String, Object> params,@PathVariable(name = "attrgroupId") Long attrGroupId){
        PageUtils pageUtils =  pmsAttrGroupService.getNoGroupAttr(params,attrGroupId);
         return R.ok().put("page",pageUtils);
     }
     /*
     * /product/attrgroup/attr/relation
     * 属性分组中的新建关联关系
     * */
     @PostMapping("/attr/relation")
     public R saveRealtion(@RequestBody AttrGroupRelation [] relation){
       pmsAttrGroupService.saveRelation(relation);
        return R.ok();
     }
     /*
     * 发布商品时需要查询出当前分类下的所有分组属性以及分组下的基本属性
     *   /product/attrgroup/{catelogId}/withattr
     * */
     @GetMapping("/{catelogId}/withattr")
     public R getAttrGroupWithAttrs(@PathVariable(name = "catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo>  attrGroupWithAttrsVos = pmsAttrGroupService.getAttrGroupWithAttrs(catelogId);
         return R.ok().put("data",attrGroupWithAttrsVos);
     }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
  //  @RequiresPermissions("product:pmsattrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
	//	PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);
        PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getByIdWithCategory(attrGroupId);//查询时需要查询出分类路径
        return R.ok().put("attrGroup", pmsAttrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("product:pmsattrgroup:save")
    public R save(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.save(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:pmsattrgroup:update")
    public R update(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.updateById(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:pmsattrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		pmsAttrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
